package com.midou.administration.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.midou.administration.bo.CommonResult;
import com.midou.administration.bo.UserBo;
import com.midou.administration.bo.uniApp;
import com.midou.administration.entity.Users;
import com.midou.administration.mapper.UsersMapper;
import com.midou.administration.service.impl.UsersServiceImpl;
import com.midou.administration.bo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/administration/users")
public class UsersController {

    @Autowired
    private UsersServiceImpl user;

    @Autowired
    private UsersMapper mapper;


    //用户登录
    @RequestMapping("/login")
    public Msg log(@RequestBody Users users){
        return user.login(users.getUserCode(),users.getUserPassword());
    }

    //注册一个新的用户
    @RequestMapping("/register")
    public int register(@RequestBody Users users){
        int  i = this.user.insert(users);
        return i;
    }

    //发送验证码
    @RequestMapping("/send")
    public Msg sel(@RequestBody uniApp app){

        return  user.send(app.getPhone());
    }

    //验证 验证码是否正确
    @RequestMapping("/verify")
    public int verify(@RequestBody uniApp app){
        System.out.println(app.getCode());
//        return 0;
        return this.user.verify(app.getPhone(),app.getCode());
    }

    //修改用户密码
    @RequestMapping("/chanpwd")
    public int updatePwd(@RequestBody uniApp uniApp){
        return  0;
    }

    /*判断该账户是否被注册过*/
    @RequestMapping("/repetition")
    public Msg repetition(@RequestBody Users users){
        return this.user.repetition(users);
    }

    //显示所有用户信息
    @RequestMapping("/userMember")
    public Page userMember(@RequestBody UserBo userBo){
        return user.userMember(userBo);
    }

    //修改前根据ID查询要修改的会员信息
    @RequestMapping("updateUserSelect")
    public Users updateUserSelect(@RequestBody Integer userId){
        return user.updateUserSelect(userId);
    }
    //修改会员信息
    @RequestMapping("updateNemBer")
    public CommonResult updateNemBer(@RequestBody Users users){
        if(users.getUserPassword().equals("")){
            return new CommonResult(500,"密码不能为空");
        }
        System.out.println(users.getUserPassword().length());
        if(users.getUserPassword().length()<6||users.getUserPassword().length()>16){
            return new CommonResult(500,"密码长度必须为6-16位");
        }
        if(users.getUserPhone().equals("")){
            return new CommonResult(500,"手机号不能为空");
        }
        if(users.getUserPhone().length()!=11){
            return new CommonResult(500,"手机号必须为11位");
        }
        if(users.getUserPaymentCode().length()!=6){
            return new CommonResult(500,"支付密码长度必须为6位");
        }


        Integer num=user.updateNemBer(users);
        if(num>0){
            return new CommonResult(200,"修改成功");
        }
        return null;
    }

    //添加会员信息
    @RequestMapping("saveVip")
    public CommonResult saveVip(@RequestBody Users users){
        if(users.getUserCode()==null){
            return new CommonResult(500,"会员账号不能为空");
        }
        List<Users> users1 =user.userCode(users.getUserCode(),users.getUserBusinessId());
        if(users1.size()>0){
            return new CommonResult(500,"会员号重复了");
        }
        if(users.getUserName()==null){
            return new CommonResult(500,"会员名称不能为空");
        }
        if(users.getUserPaymentCode()==null){
            return new CommonResult(500,"支付密码不能为空");
        }
        if(users.getUserPaymentCode().length()!=6){
            return new CommonResult(500,"支付密码必须是6位数");
        }
        if(users.getUserPassword()==null){
            return new CommonResult(500,"密码不能为空");
        }
        if(users.getUserPassword().length()<6||users.getUserPassword().length()>16){
            return new CommonResult(500,"密码长度必须为6-16位");
        }
        if(users.getUserPhone()==null){
            return new CommonResult(500,"手机号不能为空");
        }
        if(users.getUserPhone().length()!=11){
            return new CommonResult(500,"手机号必须为11位");
        }
       List<Users> users2=user.userPhone(users.getUserPhone(),users.getUserBusinessId());
        if(users2.size()>0){
            return new CommonResult(500,"该手机号已经被注册过了");
        }
        users.setUserBirthday(new Date());
        users.setUserLevel("1");
        users.setUserIntegral(0);
        users.setUserSex("男");
        users.setUserMoney(0.0);
        users.setUserDiscount(10.f);
        users.setRoleId(5);
        users.setUserCumulative(0.0);
        int random= (int) (Math.random()*4+1);
        String price[]={"https://z3.ax1x.com/2021/04/07/c3hzs1.jpg","https://z3.ax1x.com/2021/04/07/c349Z6.jpg","https://z3.ax1x.com/2021/04/07/c34ARH.jpg","https://z3.ax1x.com/2021/04/07/c34ZQA.jpg"};
        users.setUserPicture(price[random]);
        Integer num=user.saveVip(users);
        if(num>0){
            return new CommonResult(200,"添加成功");
        }

        return null;
    }
    //会员充值
    @RequestMapping("vipChongQian")
    public CommonResult vipChongQian(@RequestBody UserBo users){
        return user.vipChongQian(users);
    }
    //删除会员
    @RequestMapping("deleteMenBer")
    public CommonResult deleteMenBer(@RequestBody Integer id){
        return user.deleteMenBer(id);
    }



    /*用户修改密码*/
    @RequestMapping("/updPwdByid")
    public Msg updPwdById(@RequestBody uniApp uniApp){
        System.out.println("更改密码");
        System.out.println(uniApp.getNewPwd());
        System.out.println(uniApp.getPhone());
        System.out.println(uniApp.getUserId());
        //修改密码
        return this.user.updPwdById(uniApp);
    }

    //查询价格
    @RequestMapping("/jsjg")
    public Users jsjg(@RequestBody String phone){
        return user.cxjg(phone);
    }
    //vip消费
    @RequestMapping("/vipZhiFu")
    public CommonResult vipZhiFu(@RequestBody UserBo userBo){
        return user.vipZhiFu(userBo);
    }

}
